package com.example.kaogong.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.sys.pojo.CourseVideo;
import com.example.kaogong.sys.mapper.CourseVideoMapper;
import com.example.kaogong.sys.pojo.vo.CourseVideoVo;
import com.example.kaogong.sys.service.ICourseVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  课程视频服务实现类
 * </p>
 *
 * @author  
 * @since 2022-03-29
 */
@Service
public class CourseVideoServiceImpl extends ServiceImpl<CourseVideoMapper, CourseVideo> implements ICourseVideoService {
    @Autowired
    private CourseVideoMapper courseVideoMapper;

    @Value("${qiniu.url}")
    private String urlHead;

    @Override
    public List<CourseVideoVo> getCourseVideoListByCourseId(Integer courseId) {
        List<CourseVideoVo> courseVideoVoList = courseVideoMapper.getCourseVideoListByCourseId(courseId);
        courseVideoVoList.forEach(courseVideoVo -> {
            courseVideoVo.setVideoUrl(urlHead + courseVideoVo.getVideoUrl());
        });
        return courseVideoVoList;
    }

    /**
     * 新增课程视频
     * @param courseVideo
     * @return
     */
    @Override
    public RespBean addCourseVideo(CourseVideo courseVideo) {
        int insert = courseVideoMapper.insert(courseVideo);
        if (insert < 1)
            return RespBean.error();
        return RespBean.success();
    }

    /**
     * 根据视频编号删除课程视频
     * @param videoId
     * @return
     */
    @Override
    public RespBean deleteCourseVideo(Integer videoId) {
        int delete = courseVideoMapper.delete(new QueryWrapper<CourseVideo>().eq("video_id", videoId));
        if (delete < 1)
            return RespBean.error();
        return RespBean.success();
    }
}
